import net from '@/network/net.js';
import $ from 'jquery';
import store from '@/store/store.js';

let api = {
    'getCarBrand': '/app/car/v1/getCarBrand',
    'getCarList': '/app/car/v1/getCarList',
    'getCarDetail': '/app/car/v1/getCarDetail',
    'saveCarBase': '/app/car/v1/saveCarBase',
    'saveCarPro': '/app/car/v1/saveCarPro',
    'saveCarExt': '/app/car/v1/saveCarExt',
    'saveCarReport': '/app/car/v1/saveCarReport',
    'delCar': '/app/car/v1/delCar',
};

let postCfg = {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: function(data) {
        return $.param(data)
    }
}

let res = {

};

function getCarBrand(carId) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.getCarBrand, {
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function getCarList(page, pageSize, name) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.getCarList, {
            page: page,
            pageSize: pageSize,
            name: name
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function getCarDetail(carId) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.getCarDetail, {
            carId: carId
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}


function saveCarBase(id,icon,name,engineNo,vehicleLicense,km,year,quotePrice,downPrice,factoryPrice,
    isShelf,isSellOut,transferNum) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.saveCarBase, {
            id: id,
            icon: icon,
            name: name,
            engineNo: engineNo,
            vehicleLicense: vehicleLicense,
            km: km,
            year: year,
            quotePrice: quotePrice,
            downPrice: downPrice,
            factoryPrice: factoryPrice,
            isShelf: isShelf,
            isSellOut: isSellOut,
            transferNum: transferNum
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function saveCarPro(id,brandId,plateSite,gearBox,volume,seatNum,volumeStand,color,
    fuelType,driveType,differCountry,motoType) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.saveCarPro, {
            id: id,
            brandId: brandId,
            plateSite: plateSite,
            gearBox: gearBox,
            volume: volume,
            seatNum: seatNum,
            volumeStand: volumeStand,
            color: color,
            fuelType: fuelType,
            driveType: driveType,
            differCountry: differCountry,
            motoType: motoType
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function saveCarExt(id,carBrief,imgBrief){
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.saveCarExt, {
            id: id,
            carBrief: carBrief,
            imgBrief: imgBrief
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function saveCarReport(id,content,checkItem){
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.saveCarReport, {
            id: id,
            content: content,
            checkItem: checkItem
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function delCar(id){
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.delCar, {
            carId: id,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

export default {
    getCarBrand,
    getCarList,
    getCarDetail,
    saveCarBase,
    saveCarPro,
    saveCarExt,
    saveCarReport,
    delCar
}
